{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b3640bee-db06-48e8-8a4a-3b3b32e43bd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import fiftyone as fo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "500116dd-6ff3-47fe-9fd9-fbb65cc6ed00",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "print(fo.list_datasets())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4d26f2f7-6bc1-4272-a181-bd72fc5658fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading split 'validation' to '/home/lingxiao/fiftyone/coco-2017/validation' if necessary\n",
      "Found annotations at '/home/lingxiao/fiftyone/coco-2017/raw/instances_val2017.json'\n",
      "Sufficient images already downloaded\n",
      "Existing download of split 'validation' is sufficient\n",
      "Loading 'coco-2017' split 'validation'\n",
      " 100% |█████████████████| 349/349 [872.1ms elapsed, 0s remaining, 400.6 samples/s]      \n",
      "Dataset 'coco_validation' created\n"
     ]
    }
   ],
   "source": [
    "dataset = fo.zoo.datasets.load_zoo_dataset(\n",
    "    'coco-2017', \n",
    "    split='validation',\n",
    "    label_types=['detections'],\n",
    "    classes=[\"cat\", \"dog\"],\n",
    "    dataset_name='coco_validation'\n",
    ")\n",
    "dataset.compute_metadata()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f65e8c59-ab49-4c58-a654-027a0e994122",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name:        coco_validation\n",
      "Media type:  image\n",
      "Num samples: 349\n",
      "Persistent:  False\n",
      "Tags:        ['validation']\n",
      "Sample fields:\n",
      "    id:           fiftyone.core.fields.ObjectIdField\n",
      "    filepath:     fiftyone.core.fields.StringField\n",
      "    tags:         fiftyone.core.fields.ListField(fiftyone.core.fields.StringField)\n",
      "    metadata:     fiftyone.core.fields.EmbeddedDocumentField(fiftyone.core.metadata.Metadata)\n",
      "    ground_truth: fiftyone.core.fields.EmbeddedDocumentField(fiftyone.core.labels.Detections)\n"
     ]
    }
   ],
   "source": [
    "print(dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a893516c-3170-43ea-9158-beb0a61bb91e",
   "metadata": {},
   "outputs": [],
   "source": [
    "sample = dataset.first()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3be55e92-47d5-419d-beb6-160b980d388d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dog\n",
      "[0.07124, 0.2275449101796407, 0.5067, 0.6579640718562874]\n",
      "<Detection: {\n",
      "    'id': '61b913a91533aaa4eb85b1fd',\n",
      "    'attributes': BaseDict({}),\n",
      "    'tags': BaseList([]),\n",
      "    'label': 'dog',\n",
      "    'bounding_box': BaseList([\n",
      "        0.07124,\n",
      "        0.2275449101796407,\n",
      "        0.5067,\n",
      "        0.6579640718562874,\n",
      "    ]),\n",
      "    'mask': None,\n",
      "    'confidence': None,\n",
      "    'index': None,\n",
      "    'supercategory': 'animal',\n",
      "    'iscrowd': 0,\n",
      "}>\n"
     ]
    }
   ],
   "source": [
    "print(sample.ground_truth.detections[0]['label'])\n",
    "print(sample.ground_truth.detections[0]['bounding_box'])\n",
    "print(sample.ground_truth.detections[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f55cdbe5-0877-4a5d-8b7c-de395c7b8a3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<ImageMetadata: {\n",
       "    'size_bytes': None,\n",
       "    'mime_type': None,\n",
       "    'width': 500,\n",
       "    'height': 334,\n",
       "    'num_channels': None,\n",
       "}>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample.metadata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5c8d1add-d91d-4d1e-8822-27ab69ad4479",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['apple',\n",
       " 'backpack',\n",
       " 'banana',\n",
       " 'baseball bat',\n",
       " 'bed',\n",
       " 'bench',\n",
       " 'bicycle',\n",
       " 'bird',\n",
       " 'boat',\n",
       " 'book',\n",
       " 'bottle',\n",
       " 'bowl',\n",
       " 'bus',\n",
       " 'cake',\n",
       " 'car',\n",
       " 'carrot',\n",
       " 'cat',\n",
       " 'cell phone',\n",
       " 'chair',\n",
       " 'clock',\n",
       " 'couch',\n",
       " 'cow',\n",
       " 'cup',\n",
       " 'dining table',\n",
       " 'dog',\n",
       " 'donut',\n",
       " 'elephant',\n",
       " 'fire hydrant',\n",
       " 'fork',\n",
       " 'frisbee',\n",
       " 'handbag',\n",
       " 'horse',\n",
       " 'keyboard',\n",
       " 'kite',\n",
       " 'knife',\n",
       " 'laptop',\n",
       " 'motorcycle',\n",
       " 'mouse',\n",
       " 'orange',\n",
       " 'oven',\n",
       " 'person',\n",
       " 'potted plant',\n",
       " 'refrigerator',\n",
       " 'remote',\n",
       " 'scissors',\n",
       " 'sheep',\n",
       " 'sink',\n",
       " 'skateboard',\n",
       " 'skis',\n",
       " 'spoon',\n",
       " 'sports ball',\n",
       " 'stop sign',\n",
       " 'suitcase',\n",
       " 'surfboard',\n",
       " 'teddy bear',\n",
       " 'tie',\n",
       " 'toilet',\n",
       " 'toothbrush',\n",
       " 'traffic light',\n",
       " 'truck',\n",
       " 'tv',\n",
       " 'umbrella',\n",
       " 'vase',\n",
       " 'wine glass']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.distinct('ground_truth.detections.label')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "832d7c44-81ed-4405-8374-d993a25c8f58",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
